package com.gopay.bis.ccb.bcdl.operation;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;

import com.gopay.bis.common.trace.PaymentLog;
import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.constants.bank.BankLogConstants;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CcbConstants;
import com.gopay.common.domain.bank.BisBankReturnVo;
import com.gopay.common.domain.bank.ccb.bcdl.BaseReqBody;
import com.gopay.common.domain.bank.ccb.bcdl.person.res.TransResPersonBody;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;

public class CcbBcdlPersonHelper extends CcbBcdlBaseHelper<TransResPersonBody> {
	//kafka开关
    private String kafkaSwitch;
    
	public TransResPersonBody pay(BaseReqBody req,SocketTransport bcdlSocketTransport) throws GopayException{
		req.setTxCode("6W1303");
		String xml = getXml(req);
		logger.error("CcbBcdlPersonHelper pay xml = "+ LoggerMaskUtils.maskString(xml));
		bcdlSocketTransport.setHost(req.getUrl());
		String resXml = (String) bcdlSocketTransport.submit(xml);
		logger.error("CcbBcdlPersonHelper pay resXml = "+ LoggerMaskUtils.maskString(resXml));
		if(StringUtils.isNotBlank(resXml)){
			TransResPersonBody body = parse(resXml);
			saveEftPKafkaInfo(req,body,resXml);
			if(body!=null){
				String resultCode = body.getReturnCode()==null?"":body.getReturnCode();
				if(resultCode.equals(CcbConstants.BCDL_RESULT_CODE_SUCC)){
					//响应码为000000则代表付款成功。
					return body;
				}else{
					//响应码不是000000，可通过调用6W0500或6W0600进一步确认交易是否成功，然后决定是否重新发起
					logger.error("CcbBcdlPersonHelper error orderId = " + req.getRequestSn()+ "rspcod =" + resultCode +", returnMsg ="+ body.getReturnMsg());
					return body;
				}
			}else{
				logger.error("CcbBcdlPersonHelper parse error orderId = "+ req.getRequestSn());
			}
		}else{
			logger.error("CcbBcdlPersonHelper resXml is null orderId = "+ req.getRequestSn());
		}
		throw new GopayException();
	
	}

	/**
	 * 将对私付款返回的报文记录到kafka中
	 * @param req
	 * @param body
	 * @param resXml
	 */
	private void saveEftPKafkaInfo(BaseReqBody baseMsg, TransResPersonBody body, String plainText) {
		try {
			BisBankReturnVo vo = new BisBankReturnVo();
			//国付宝订单号
			vo.setGopayOrderId(baseMsg.getOrderId());
			//银行返回订单号
			vo.setBankOrderId(baseMsg.getOutOrderId());
			//银行渠道
			vo.setBankChannel(BisBankInterfaceChannel._00.value);
			//银行编码
			vo.setBankCode(BankLogConstants.BANK_LOG_BCDL_P_EFT_CCB);
			//银行返回错误的xml文件信息
			vo.setBankReturnXml(plainText);
			if(body!=null){
				//银行返回错误码
				vo.setRsCode(body.getReturnCode()); //指令状态
				//银行返回的错误信息
				vo.setRsMsg(body.getReturnMsg()); //交易返回描述
			}
			//银行报文订单返回时间
			vo.setBankReturnDate(DateUtils.getCurrDate("yyyy-MM-dd HH:mm:ss"));
			//报文返回记录日志时间  时间格式：yyyy-MM-dd HH:mm:ss
			vo.setCreateTsErr(DateUtils.getCurrDate("yyyy-MM-dd HH:mm:ss"));
			//保存到kafka中
			PaymentLog.info(vo,kafkaSwitch);
		} catch (Exception e) {
			logger.error("建行对私付款返回的报文记录到kafka异常："+e);
		}
		
	}

	public String getKafkaSwitch() {
		return kafkaSwitch;
	}

	public void setKafkaSwitch(String kafkaSwitch) {
		this.kafkaSwitch = kafkaSwitch;
	}
	
	
}
